package com.techwells.taco.net;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

public class HttpHandler extends Handler {

	private Context context;
	private ProgressDialog progressDialog;
	private String requestMethod;

	public String getRequestMethod() {
		return requestMethod;
	}

	public void setRequestMethod(String requestMethod) {
		this.requestMethod = requestMethod;
	}

	public HttpHandler(Context context) {
		this.context = context;
	}

	protected void start() {
		progressDialog = ProgressDialog.show(context, "请等待...", "加载中...", true);
		progressDialog.setCancelable(true);
	}

	protected void succeed(JSONObject jObject) {
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
	}

	protected void result(Object obj) {
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
	}

	protected void failed(JSONObject jObject) {
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
	}

	protected void succeed(JSONArray jArray) {
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
	}

	protected void failed(JSONArray jArray) {
		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
	}

	protected void otherHandleMessage(Message message) {
	}

	public void handleMessage(Message message) {
		switch (message.what) {
		case HttpConnectionUtils.DID_START: // connection start
			Log.d(context.getClass().getSimpleName(),
					"http connection start...");
			start();
			break;
		case HttpConnectionUtils.DID_SUCCEED: // connection success
			progressDialog.dismiss();
			String response = (String) message.obj;
			Log.d(context.getClass().getSimpleName(), "http connection return."
					+ response);
			try {

				
				if (response.startsWith("{\"state\"")) {

					JSONObject jObject = new JSONObject(response == null ? ""
							: response.trim());
					String error = "";
					if (200 == jObject.getInt("state")) { // operate success
					// Toast.makeText(context,
					// "operate succeed:",Toast.LENGTH_SHORT).show();
						succeed(jObject);
					} else {
						
						switch (jObject.getInt("state")) {
						case 400:
							error = "错误"+400;
							break;
						case 401:
							error = "错误"+401;
							{
								switch (jObject.getInt("code")) {
								case 10001:
									error+= "，"+"用户不存在";
									break;
								case 10002:
									error+= "，"+"密码错误";
									break;
								case 10003:
									error+= "，"+"验证码错误";
									break;
								case 10004:
									error = "错误"+10004;
									break;

								default:
									break;
								}
							
							}
							break;
						case 460:
							error = "错误"+460;
							break;
//						case 10001:
//							error = "用户不存在";
//							break;
//						case 10002:
//							error = "密码错误";
//							break;
//						case 10003:
//							error = "验证码错误";
//							break;
//						case 10004:
//							error = "错误"+10004;
//							break;

						default:
							break;
						}

						 Toast.makeText(context,error,Toast.LENGTH_LONG).show();
						failed(jObject);
					}

				} else {
					JSONArray jArray = new JSONArray(response == null ? ""
							: response.trim());

					succeed(jArray);

				}
			} catch (JSONException e1) {
				if (progressDialog != null && progressDialog.isShowing()) {
					progressDialog.dismiss();
				}
//				SaveSP.saveUser(context, 0);
				e1.printStackTrace();
				Toast.makeText(context, "服务器请求失败，请重新请求", Toast.LENGTH_LONG)
						.show();
			}

			if (NetworkConfig.METHOD_REGISTER_DEVICE.equals(requestMethod)) {
				//
			} else if (NetworkConfig.METHOD_GET_DEVICE_LIST
					.equals(requestMethod)) {
				// 获取设备列表
			} else if (NetworkConfig.METHOD_GET_CAR_LIST.equals(requestMethod)) {

			} else if (NetworkConfig.METHOD_REGISTER_CAR.equals(requestMethod)) {

			}

			break;
		case HttpConnectionUtils.DID_ERROR: // connection error
			if (progressDialog != null && progressDialog.isShowing()) {
				progressDialog.dismiss();
			}
			Exception e = (Exception) message.obj;
			e.printStackTrace();
			Log.e(context.getClass().getSimpleName(),
					"connection fail." + e.getMessage());
			Toast.makeText(context, "连接失败,请检查你的网络连接!", Toast.LENGTH_LONG)
					.show();
			break;
		}
		otherHandleMessage(message);
	}

}
